import json
import time

from ScrapyObject.spiders.utils.aa import read_json_file, spiderInfo, VideoBean


class RoomListBean:
    timeStamp = 0
    data = []

    def __init__(self, timeStamp, data):
        self.timeStamp = timeStamp
        self.data = data


class RoomListItemBean:
    videoTag = ""
    videoUrl = ""

    def __init__(self, videoTag, videoUrl):
        self.videoTag = videoTag
        self.videoUrl = videoUrl


# 3把最终结果分解成几份
if __name__ == '__main__':
    print('开始')
    timestamp = int(time.time())
    jsonStr = ""
    jsonStr = read_json_file('D:\\8\\' + spiderInfo.name + '.json')
    print("原始数据一共: " + str(len(jsonStr)))
    videoList = []
    roomList = []
    for i in jsonStr:
        video = VideoBean()
        video.id = 0
        video.name = i['name']
        video.url = i['url']
        video.tags = i['tags']
        video.pUrl = i['pUrl']
        video.vUrl = i['vUrl']
        videoList.append(video)
    jsonStr.clear()
    videoTag = []
    videoListTwo = []
    for i in videoList:
        if i.tags not in videoTag:
            videoTag.append(i.tags)
    for index, item in enumerate(videoTag):
        roomList.append(RoomListItemBean(videoTag=item, videoUrl="http://101.42.171.191:8083/GameServer/houtai/1/" + str(spiderInfo.name) + "/" + str(index) + ".json"))
        if len(videoListTwo) > 0:
            videoListTwo.clear()
        for i in videoList:
            if i.tags == item:
                videoListTwo.append(i)
        videoFinalList = []
        for i in videoListTwo:
            tinydict = {'id': 0, 'name': i.name, 'url': i.url, 'tags': i.tags, 'pUrl': i.pUrl, 'vUrl': i.vUrl}
            videoFinalList.append(tinydict)
        # 将JSON数组转换为字符串
        json_string = json.dumps(videoFinalList, ensure_ascii=False)
        # 将字符串写入txt文件
        with open('D:\\8\\tag\\' + str(item) + '.json', 'w', encoding='utf-8') as file:
            file.write(json_string)

    endTime = int(time.time())
    print(str(endTime - timestamp))
    print('结束')
